﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.goods
{
    public partial class goodsAdd : BaseCrm
    {
        readonly t_DALGoods theGoods = new t_DALGoods();
        readonly t_DALBrand theBrand = new t_DALBrand();
        t_Goods oGoods;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(eRule.GoodsAdd);
                this.BindBrandList();
                if (base.Request.QueryString["gid"] != null)
                {
                    this.buildForm();
                }
            }
        }
        void BindBrandList()
        {
            DataTable table = theBrand.GetListNoDeleted("").Tables[0];
            this.ddlBrand.DataTextField = "brandName";
            this.ddlBrand.DataValueField = "brandId";
            this.ddlBrand.DataSource = table;
            this.ddlBrand.DataBind();
            this.ddlBrand.Items.Insert(0, new ExtAspNet.ListItem("选择品牌", "-1"));
        }

        private void buildForm()
        {
            oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
            if (oGoods != null)
            {

                this.txtGoodsName.Text = oGoods.goodsName;
                this.txtGoodsCode.Text = oGoods.goodsCode;
                this.txtOrder.Text = oGoods.goodsOrder.ToString();
                this.txtGoodsPrice.Text = oGoods.goodsPrice.ToString();
                if (this.ddlBrand.Items.FindByValue(oGoods.brandId.ToString()) != null)
                {
                    this.ddlBrand.SelectedValue = oGoods.brandId.ToString();
                }
   
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["gid"] != null)
            {

                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                oGoods.goodsName = this.txtGoodsName.Text.Trim();
                oGoods.goodsCode = this.txtGoodsCode.Text.Trim();
                oGoods.goodsPrice = decimal.Parse(this.txtGoodsPrice.Text.Trim());
                oGoods.goodsOrder = int.Parse(this.txtOrder.Text);
                oGoods.brandId = int.Parse(this.ddlBrand.SelectedItem.Value);
                theGoods.Update(oGoods);
                ExtAspNet.Alert.Show("商品修改成功！", "", "openNewTab('GoodsManager','/crm/goods/goodsManager.aspx','商品管理','GoodsAdd')");
            }
            else
            {
                //this.UploadPhoto();
                oGoods = new t_Goods();
                oGoods.goodsName = this.txtGoodsName.Text.Trim();
                oGoods.goodsCode = this.txtGoodsCode.Text.Trim();
                oGoods.goodsPrice = decimal.Parse(this.txtGoodsPrice.Text.Trim());
                oGoods.goodsOrder = int.Parse(this.txtOrder.Text);
                oGoods.brandId = int.Parse(this.ddlBrand.SelectedItem.Value);
                theGoods.Add(oGoods);

                ExtAspNet.Alert.Show("商品添加成功！", "", "openNewTab('GoodsManager','/crm/goods/goodsManager.aspx','商品管理','GoodsAdd')");
            }
        }

    }
}